package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrdersMapper;
import com.itheima.domain.Orders;
import com.itheima.factory.MapperFactory;
import com.itheima.service.OrdersService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    SqlSession sqlSession =null;
    @Override
    public List<Orders> findAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
       List<Orders> list=mapper.findAll();
       sqlSession.close();
       return list;
    }

    @Override
    public Orders findById(String id) {
         sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        Orders orders=mapper.findByOid(id);
        sqlSession.close();
        return orders;
    }

    @Override
    public void delete(String[] split) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
            mapper.deleteo_dByOids(split);
            mapper.deleteByIds(split);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<Orders> findAllByLike(int pageNum, int pageSize,String dd) {
        //获取session
        SqlSession session = MapperFactory.getSqlSession();
        //获取mapper代理对象
        OrdersMapper mapper = MapperFactory.getMapper(session, OrdersMapper.class);
        //分页参数设置
        PageHelper.startPage(pageNum,pageSize);
        dd = dd.replace(" ", "");
        dd="%"+dd+"%";
        List<Orders> list=mapper.findAllByLike(dd);
        //释放资源
        TransactionUtil.close(session);
        return list;
    }
}
